﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SXMeteorological.DataSyn.RaderPic
{
    public class RadarPic
    {
        public static void GETIMG(string station)
        {
            byte[] img = null;
            string filename = string.Empty;
            string con = "data source=10.172.8.48;initial catalog=PictPro;persist security info=True;user id=sa;password=bexawlzx;";
            string sql = "select top 30* from tabRaderGif where StationNum='" + station + "'order by RadDatetime desc";
            SqlConnection conn = new SqlConnection(con);
            conn.Open();
            SqlCommand com = new SqlCommand(sql, conn);
            SqlDataReader dr = com.ExecuteReader();
            var path = @"D:\leida";
            string[] filenames = Directory.GetDirectories(@"D:\leida");
            foreach (string fname in filenames)
            {
                int fn = int.Parse(fname.Substring(9, 3));
                int ft = int.Parse(station);
                if (fn == ft)
                {
                    Directory.Delete(fname, true);
                }

            }
            path = Path.Combine(path, DateTime.Now.ToString("yyyy-MM-dd"));
            path = Path.Combine(path, station); 
            Directory.CreateDirectory(path);
            while (dr.Read())
            {

                img = (byte[])dr.GetValue(5);
                filename =Convert.ToDateTime(dr.GetValue(1)).ToString("yyyy-MM-dd HH:mm:ss.fff").Substring(8,2)+ Convert.ToDateTime(dr.GetValue(1)).ToString("yyyy-MM-dd HH:mm:ss.fff").Substring(11,2)+
                    Convert.ToDateTime(dr.GetValue(1)).ToString("yyyy-MM-dd HH:mm:ss.fff").Substring(14,2) +
                    Convert.ToDateTime(dr.GetValue(1)).ToString("yyyy-MM-dd HH:mm:ss.fff").Substring(17,6)
                    + dr.GetValue(3).ToString() + dr.GetValue(2).ToString() + dr.GetValue(4).ToString();
                Image _img = Image.FromStream(new MemoryStream(img));
                _img.Save(path + @"\" + filename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            conn.Close();
       
        }
    }
}
